package me.project.jobster.controller;

import me.project.jobster.common.constant.GlobalConstants;
import me.project.jobster.dao.mybatis.mapper.NotificationMapper;
import me.project.jobster.dao.mybatis.model.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author mbryce
 * @date 2018/4/28
 */
public class BaseController {

    @Autowired
    private NotificationMapper notificationMapper;

    public void countMsgUnread(HttpServletRequest request){
        Notification notification = new Notification();
        notification.setReceiver(String.valueOf(request.getSession().getAttribute("user_id")));
        notification.setStatus(GlobalConstants.MSG_UNREAD);
        int count = notificationMapper.selectCount(notification);
        request.getSession().setAttribute("countMsgUnread",count);
    }

    /**
     *
     * @return
     * @throws Exception
     */
    protected Map<String, Object> getParameterMap(HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, String[]> tempMap = request.getParameterMap();
        Set<String> keys = tempMap.keySet();
        try {
            for (String key : keys) {
                byte source[] = request.getParameter(key).getBytes("iso8859-1");
                String modelname = null;
                modelname = new String(source, "UTF-8");
                resultMap.put(key, modelname);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return resultMap;
    }
}
